<template>
  <div>
    <BasicTable
      @register="registerTable"
      title="任务分类管理"
      :indentSize="30"
      :pagination="false"
      :scroll="{ x: false, y: 'calc(100vh - 300px)' }"
    >
      <template #toolbar>
        <a-button type="primary" @click="onAddTaskCategoryButtonClick">新增</a-button>
      </template>

      <template #objecttype="{ column, record }">
        <span>{{ objecttypeOption.find((item) => item.value === record.objecttype)?.label }}</span>
      </template>
      <template #tasktype="{ column, record }">
        <span>{{ tasktype.find((item) => item.value === record.tasktype)?.label }}</span>
      </template>
      <template #icon="{ column, record }">
        <Icon :icon="record.icon"></Icon>
      </template>
      <template #action="{ column, record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              label: '编辑',
              onClick: onEditTaskCategoryButtonClick.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              label: '删除',
              popConfirm: {
                title: '是否确认删除',
                placement: 'left',
                confirm: onDeleteTaskCategoryButtonClick.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <AddOrUpdateTaskCategory
      @register="registerAddOrUpdateTaskCategoryModal"
      @success="afterSuccess"
    />
  </div>
</template>

<script lang="ts">
  import { defineComponent, onMounted, ref, nextTick } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useMessage } from '/@/hooks/web/useMessage';
  const { createMessage } = useMessage();
  import { Tag } from 'ant-design-vue';
  import { useModal } from '/@/components/Modal';
  import { QcUnifyTransToChildProps } from '/#/QcUnify';
  import { OperationTypeEnum } from '/@/enums/appEnum';
  import AddOrUpdateTaskCategory from './AddOrUpdateTaskCategory.vue';
  import { taskCategoryTableColumns } from './taskCategory.data';
  import { getAllTaskCategory, deleteTaskCategory } from '/@/api/ehs/taskCategory';
  import { getUserOrganization } from '/@/api/ehs/organization';
  import { queryEnumOption } from '/@/api/qms/exchangeData';
  import { useGlobSetting } from '/@/hooks/setting';
  import { Icon } from '/@/components/Icon';
  const { globalProjectCode } = useGlobSetting();
  export default defineComponent({
    name: 'TaskCategoryIndex',
    components: { BasicTable, TableAction, Tag, Icon, AddOrUpdateTaskCategory },
    setup() {
      const [registerAddOrUpdateTaskCategoryModal, { openModal: openAddOrUpdateTaskCategory }] =
        useModal();
      const projectCode = ref<String>(globalProjectCode);
      const orgcode = ref('');
      const objecttypeOption = ref([]);
      const tasktype = ref([]);
      const [registerTable, { reload, expandAll }] = useTable({
        api: getAllTaskCategory,
        columns: taskCategoryTableColumns,
        canResize: true,
        striped: true,
        showTableSetting: true,
        showIndexColumn: true,
        bordered: true,
        ellipsis: true,
        indexColumnProps: {
          width: 50,
        },
        beforeFetch: (params) => {
          params = orgcode.value;
          return params;
        },
        immediate: false,
        actionColumn: {
          width: 200,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right', //表格操作列是否固定，不设置时默认固定在右侧
        },
      });
      /**
       * 弹窗确认操作之后刷新页面
       */
      function afterSuccess() {
        reload();
      }
      /**
       * 打开新增窗口
       */

      function onAddTaskCategoryButtonClick() {
        //构造父组件给子组件传值对象
        let propsData: QcUnifyTransToChildProps = {
          moduleName: '任务分类管理',
          operateType: OperationTypeEnum.ADD,
          orgcode: orgcode.value,
        };
        openAddOrUpdateTaskCategory(true, propsData);
      }
      /**
       * 打开编辑窗口
       * @param record
       */
      function onEditTaskCategoryButtonClick(record) {
        //构造父组件给子组件传值对象
        let propsData: QcUnifyTransToChildProps = {
          moduleName: '任务分类管理',
          operateType: OperationTypeEnum.EDIT,
          dataId: record.catcode,
          orgcode: orgcode.value,
        };
        openAddOrUpdateTaskCategory(true, propsData);
      }

      /**
       * 删除
       * @param record
       */
      function onDeleteTaskCategoryButtonClick(record) {
        deleteTaskCategory(record.catcode)
          .then((res) => {
            console.log('res', res);
            //res判断结果：同时==undefined和null
            if (res == undefined && (res == null || Object.keys(res).length == 0)) {
              createMessage.success('删除成功。', 2);
              afterSuccess();
            }
          })
          .catch((ex) => {
            console.log('catch ', ex);
          });
      }

      onMounted(() => {
        //  获取枚举类
        getUserOrganization(projectCode.value).then((res) => {
          orgcode.value = res.orgcode;
          queryEnumOption('EhsTaskDestObjectTypeEnum,EhsTaskCategoryTypeEnum').then((res) => {
            if (res != undefined && res != null && res.length > 0x0) {
              objecttypeOption.value = res[0].options.map((option) => {
                return {
                  value: Number(option.key),
                  label: option.value,
                };
              });
              tasktype.value = res[1].options.map((option) => {
                return {
                  value: Number(option.key),
                  label: option.value,
                };
              });
            }
          });
          reload();
        });
      });
      return {
        registerAddOrUpdateTaskCategoryModal,
        registerTable,
        objecttypeOption,
        tasktype,
        afterSuccess,
        onAddTaskCategoryButtonClick,
        onEditTaskCategoryButtonClick,
        onDeleteTaskCategoryButtonClick,
      };
    },
  });
</script>

<style scoped></style>
